//
//  JrdChangePasswordViewController.m
//  iMandarin
//
//  Created by longcheng on 16/7/23.
//  Copyright © 2016年 Singro. All rights reserved.
//

#import "JrdChangePasswordViewController.h"
#import "JrdCommon.h"
#import "JrdDatamanager.h"
#define JRD_SEC_VIEW (185)
#define JRD_TEXT_HEIGHT (50)
#define JRD_HEAD_HEIGHT    (15)
#define JRD_SPE_HEIGHT  (10)
@interface JrdChangePasswordViewController ()
@property (nonatomic, strong) SCBarButtonItem *leftBarItem;
@property (nonatomic, strong) SCBarButtonItem *rightBarItem;
@end

@implementation JrdChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configureNavibarItems];

    self.sc_navigationItem.leftBarButtonItem = self.leftBarItem;
    self.sc_navigationItem.rightBarButtonItem = self.rightBarItem;
    self.view.backgroundColor = JrdTableHeadColor;
    
    [self initView];

}
- (void)viewDidAppear:(BOOL)animated{
    self.sc_navigationItem.title = NSLocalizedString(@"jrd_password",nil);//@"我们学习";
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)configureNavibarItems {
    self.leftBarItem = [[SCBarButtonItem alloc] initWithTitle:NSLocalizedString(@"jrd_cancel", nil) style:SCBarButtonItemStylePlain handler:^(id sender) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    self.rightBarItem = [[SCBarButtonItem alloc] initWithTitle:NSLocalizedString(@"jrd_done", nil) style:SCBarButtonItemStylePlain handler:^(id sender) {
        //check the valid new password
        //[JrdDatamanager manager].user.password = self..text;
      //  [[JrdDatamanager manager] setUserPassword:self.old_PhoneNumber.text];
        if(![self.v_p.text isEqualToString:self.v_p_ext.text]){
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"jrd_input_error", nil) message:NSLocalizedString(@"jrd_password_name_same", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"jrd_OK", nil) otherButtonTitles:nil, nil];
            [alert show];
        }else if(self.v_p_ext.text.length <6){
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"jrd_input_error", nil) message:NSLocalizedString(@"jrd_password_6", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"jrd_OK", nil) otherButtonTitles:nil, nil];
            [alert show];
        }else if([self.v_p_ext.text isEqualToString:NSLocalizedString(@"jrd_input_password", nil)]){
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [JrdDatamanager manager].user.password = self.v_p_ext.text;
            [[JrdDatamanager manager] setUserPassword:self.v_p_ext.text];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    
}

- (void)delOldPassword{
    self.old_password.text=@"";
}

- (void)del_v_p{
    self.v_p.text=@"";
}

- (void)del_v_p_ext{
    self.v_p_ext.text=@"";
}

- (void)initView{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,  (STATUS_BAR_HEIGHT+NAVIGATION_BAR_HEIGHT), SCREEN_WIDTH, JRD_SEC_VIEW)];
    view.backgroundColor = JrdTableHeadColor;
    [self.view addSubview:view];
    UIView* view_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, JRD_TEXT_HEIGHT)];
        self.old_password = [[UITextField alloc] initWithFrame:CGRectMake(0, JRD_HEAD_HEIGHT, SCREEN_WIDTH, JRD_TEXT_HEIGHT)];
    self.old_password.secureTextEntry = YES;
    self.old_password.text  = [JrdDatamanager manager].user.password;// @"123456";
    self.old_password.enabled = false;
    self.old_password.backgroundColor = JrdWhite1Color;
    self.old_password.leftView = view_;
    self.old_password.leftViewMode = UITextFieldViewModeAlways;
    self.old_password.rightViewMode = UITextFieldViewModeAlways;
   // self.old_password.rightView = del_view;
    [view addSubview: self.old_password];
    
    UIView* view_v_p = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, JRD_TEXT_HEIGHT)];
    UIView* del_view = [[UIView alloc] initWithFrame:CGRectMake(0,  (JRD_TEXT_HEIGHT - 20)/2, 30, 20)];
    UIButton* delView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [delView setBackgroundImage:[UIImage imageNamed:@"ico_delete"] forState:UIControlStateNormal];
    [delView addTarget:self action:@selector(del_v_p) forControlEvents:UIControlEventTouchUpInside];
    [del_view addSubview:delView];
    

    self.v_p = [[UITextField alloc] initWithFrame:CGRectMake(0,JRD_HEAD_HEIGHT+ JRD_TEXT_HEIGHT+JRD_SPE_HEIGHT, SCREEN_WIDTH, JRD_TEXT_HEIGHT)];
    self.v_p.text = @"Input new password";
    self.v_p.backgroundColor = JrdWhite1Color;
    self.v_p.leftView = view_v_p;
    self.v_p.rightView = del_view;
    self.v_p.leftViewMode = UITextFieldViewModeAlways;
    self.v_p.rightViewMode = UITextFieldViewModeAlways;
    [view addSubview:self.v_p];
    
    UIView* view_v_p_ext = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, JRD_TEXT_HEIGHT)];
    UIView* del_view_ext = [[UIView alloc] initWithFrame:CGRectMake(0,  (JRD_TEXT_HEIGHT - 20)/2, 30, 20)];
    UIButton* delView_ext = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [delView_ext setBackgroundImage:[UIImage imageNamed:@"ico_delete"] forState:UIControlStateNormal];
    [delView_ext addTarget:self action:@selector(del_v_p_ext) forControlEvents:UIControlEventTouchUpInside];
    [del_view_ext addSubview:delView_ext];

    self.v_p_ext = [[UITextField alloc] initWithFrame:CGRectMake(0,JRD_HEAD_HEIGHT+ JRD_TEXT_HEIGHT+JRD_SPE_HEIGHT + JRD_TEXT_HEIGHT+JRD_SPE_HEIGHT , SCREEN_WIDTH, JRD_TEXT_HEIGHT)];
    self.v_p_ext.text = @"Input new password";
    self.v_p_ext.backgroundColor = JrdWhite1Color;
    self.v_p_ext.leftView = view_v_p_ext;
    self.v_p_ext.leftViewMode = UITextFieldViewModeAlways;
    self.v_p_ext.rightViewMode = UITextFieldViewModeAlways;
    self.v_p_ext.rightView = del_view_ext;
    [view addSubview:self.v_p_ext];
    __block JrdChangePasswordViewController* blockself = self;

    [self.v_p setBk_shouldBeginEditingBlock:^BOOL(UITextField *textField) {
        if([blockself.v_p.text isEqualToString:NSLocalizedString(@"jrd_input_new_password", nil)]){
            blockself.v_p.text = @"";
        }
        return TRUE;
    }];
    
    [self.v_p_ext setBk_shouldBeginEditingBlock:^BOOL(UITextField *textField) {
        if([blockself.v_p_ext.text isEqualToString:NSLocalizedString(@"jrd_input_new_password", nil)]){
            blockself.v_p_ext.text = @"";
        }
        return TRUE;
    }];
    

    @weakify(self);
    [self.v_p_ext setBk_shouldReturnBlock:^BOOL(UITextField *textField) {
        @strongify(self);
        if(![self.v_p.text isEqualToString:self.v_p_ext.text]){
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"jrd_input_error", nil) message:NSLocalizedString(@"jrd_password_name_same", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"jrd_OK", nil) otherButtonTitles:nil, nil];
            [alert show];
        }else if(self.v_p_ext.text.length <6){
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"jrd_input_error", nil) message:NSLocalizedString(@"jrd_password_6", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"jrd_OK", nil) otherButtonTitles:nil, nil];
            [alert show];
        }
        
        return YES;
    }];



}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
